//
//  BBProduct.swift
//  BBLinkiOSProject
//
//  Created by liuzhu on 2018/12/12.
//  Copyright © 2018 liuzhu. All rights reserved.
//

import UIKit

class BBProduct: NSObject {
   @objc var categoryId:String?
   @objc var productId:String?
   @objc var productName:String?
   @objc var subName:String?
   @objc var productImages:String?
   @objc var showImages:String?
   @objc var price:NSNumber?
   @objc var stock:NSNumber?
   @objc var status:NSNumber?
   @objc var reserveStr1:String?
    
    //字典转模型
    init(dict:[String:AnyObject]){
        super.init()
        setValuesForKeys(dict)
    }
    
    override func setValue(_ value: Any?, forUndefinedKey key: String) {
        print(key)
    }
    
    class func loadData(finish:@escaping (_ models:[BBProduct]?,_ error:Error?)->()){
        let path = "shop/list.do"
        AFNetWorkManager.shareNetworkTools().post(path, parameters: nil, success: { (_, JSON) in
            let dict = JSON as![String:AnyObject]
            let dictStatus = dict["status"]as!NSNumber
            if(dictStatus == 0){
                let data = dict["data"]
                let models = dict2Model(list: data!["list"] as! [[String:AnyObject]])
                finish(models,nil)
            }else{
                finish(nil,nil)
            }
        }) { (_, error) in
            finish(nil,error)
        }
    }
    
    //将字段数组转换为模型数组
    class func dict2Model(list:[[String:AnyObject]])->[BBProduct]{
        var models = [BBProduct]()
        for dict in list{
            models.append(BBProduct(dict: dict))
        }
        return models
    }
}
